<?php

/**
 * SQLite数据库连接驱动
 */
class SQLite implements DBInterface {

    private $link = null;

    public function __construct() {
        if (!function_exists('sqlite_open'))
            throw new DBExcepton('no sqlite_open function');
    }

    public function fetchArray($result) {
        return sqlite_fetch_array($result);
    }

    public function getConection($dsn) {
        $dbfile = S_ROOT . '/' . $dsn['host'] . $dsn['path'];

        if (!file_exists(S_ROOT . '/' . $dsn['host'])) {
            @mkdir(S_ROOT . '/' . $dsn['host'], 0777, 1);
        }

        $this->link = @sqlite_open($dbfile, 0666, $error);
        if (!$this->link)
            throw new DBExcepton($error);
    }

    public function query($sql) {
        $result = @sqlite_query($this->link, $sql, SQLITE_ASSOC, $error);
        if (!$result)
            throw new DBExcepton($error);
        return $result;
    }

    public function version() {
        return 2.1;
    }

    public function __destruct() {
        if ($this->link)
            sqlite_close($this->link);
    }

}

?>
